library(tidyverse)
library(readr)
chic <- read_csv("ggplot2/ggModify/chicago-nmmaps.csv")

# 1. 改变面板背景颜色
#   要改变面板区域(即绘制数据的区域)的背景颜色(填充)，需要调主题theme中的panel.background:
ggplot(chic, aes(x = date, y = temp, color = season)) +
  geom_point() +
  labs(x = "Year", y = "Temperature (°F)", color = "Season") +
  theme(panel.background = element_rect(fill = "#64D2AA", color = "#64AABB", size = 2))

# 2. 改变网格线
#   有两种类型的网格线: 一级网格线（major grid line）和二级网格线(minor grid line)。
#   你可以通过覆盖面板的默认值来更改这些。
#   对于每一组网格线，分别使用panel.grid.major和panel.grid.minor。
#   要改变一级网格线的颜色和大小，使用panel.grid.major.x和panel.grid.major.y。
#   要改变二级网格线的颜色和大小，使用panel.grid.minor.x和panel.grid.minor.y。
ggplot(chic, aes(x = date, y = temp, color = season)) +
  geom_point() +
  labs(x = "Year", y = "Temperature (°F)") +
  theme(
    panel.grid.major = element_line(color = "gray10", size = 1),
    panel.grid.minor = element_line(color = "gray50", size = 0.5)
  )

#   你甚至可以为所有的网格线指定4个不同水平的设置:
ggplot(chic, aes(x = date, y = temp, color = season)) +
  geom_point() +
  labs(x = "Year", y = "Temperature (°F)") +
  theme(
    panel.grid.major = element_line(linetype = "dashed", size = 1),
    panel.grid.minor = element_line(linetype = "dotted", size = 0.5),
    panel.grid.major.x = element_line(color = "red1"),
    panel.grid.major.y = element_line(color = "blue1"),
    panel.grid.minor.x = element_line(color = "red4"),
    panel.grid.minor.y = element_line(color = "blue4")
  )

# 删除一些或所有网格线:
ggplot(chic, aes(x = date, y = temp)) +
  geom_point(color = "firebrick") +
  labs(x = "Year", y = "Temperature (°F)") +
  theme(panel.grid.minor = element_blank())

ggplot(chic, aes(x = date, y = temp)) +
  geom_point(color = "firebrick") +
  labs(x = "Year", y = "Temperature (°F)") +
  theme(panel.grid = element_blank())

# 3. 改变网格线的间距
#   可以定义一级网格线和二级网格线之间的断点:
ggplot(chic, aes(x = date, y = temp)) +
  geom_point(color = "firebrick") +
  labs(x = "Year", y = "Temperature (°F)") +
  scale_y_continuous(breaks = seq(0, 100, 10),
                     minor_breaks = seq(0, 100, 2.5))


# 4. 更改图表背景颜色
#   同样，要改变plot区域的背景颜色(填充)，需要修改主题元素plot.background:
ggplot(chic, aes(x = date, y = temp)) +
  geom_point(color = "firebrick") +
  labs(x = "Year", y = "Temperature (°F)") +
  theme(plot.background = element_rect(fill = "gray60",
                                       color = "gray30", size = 2))
